﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
using com.baidu.ai;
using Newtonsoft.Json;

/// <summary>
/// ReturnResultDetect 的摘要说明
/// </summary>
namespace com.baidu.ai
{
    public static class ReturnResultDetect
    {
        public static int GetFaceAddReturnResult(string strjson)
        {
            int BackResult = 0;
            SearchReturn feedback = JsonConvert.DeserializeObject<SearchReturn>(strjson);
            //JavaScriptSerializer Jss = new JavaScriptSerializer();
            //Dictionary<string, object> DicText = (Dictionary<string, object>)Jss.DeserializeObject(strjson);
            if (feedback.result==null)
            {
                BackResult = 0;
            }
            else
            {
                BackResult = 1;
            }

            //Session["Token"] = Access_Token;
            //Response.Write(Access_Token);
            return BackResult;
        }
        public static string GetFaceSearchReturnResult(string strjson)
        {
            string BackResult ="";
            int score = 0;
            //string flag = "";
            SearchReturn feedback = JsonConvert.DeserializeObject<SearchReturn>(strjson);
            if (feedback.result==null)
            {
                BackResult = "_0";   //未检测到人脸
            }
            else
            {
                score = Convert.ToInt32(feedback.result.user_list[0].score);
                if (score<80)
                {
                    BackResult = "_1";   //人脸不匹配
                }
                else
                {
                    BackResult = feedback.result.user_list[0].user_id;
                }
            }
            
            //BackResult = feedback.result.user_list[0].user_id;
            //JObject obj = JObject.Parse(strjson);
            //Console.WriteLine(obj["result"]["user_id"][1]);
            //BackResult = obj["result"]["user_id"].ToString();

            //JavaScriptSerializer Jss = new JavaScriptSerializer();
            //Dictionary<string, object> DicText = (Dictionary<string, object>)Jss.DeserializeObject(strjson);

            //if (DicText.ContainsKey("face_token") /*&& DicText["error_msg"].ToString() == "SUCCESS"*/)
            //{
            //    BackResult = DicText["result"].ToString();
            //    //if (score>=80)
            //    //{
            //    //    BackResult = DicText["user_id"].ToString();
            //    //}   
            //}

            return BackResult;
        }
    }
}
